<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/CustomerCom.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/C_serviceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");

$common = new Common();
$customercom = new CustomerCom();
$ordercom = new OrderCom();
$servicecom = new ServiceCom();
$serviceinfocom = new ServiceinfoCom();
$cservicecom = new CservicetCom();

$appid = "wx6cb19f01728284e9";
$secret = "03932f973d78d46b88986cc10cba05db";
$mch_id = "1303559301";
$paykey = "03932f973d78d46b88986cc10cba05db";

//$fee = $_REQUEST['totalfee'] ? $_REQUEST['totalfee'] : 0;
//if ($fee == 0) {
//    echo json_encode(array("code" => 100, "msg" => "get fee failed"));
//    exit();
//}
//$tradeno = $_REQUEST['tradeno'] ? $_REQUEST['tradeno'] : null;
//if(empty($tradeno)){
//    echo json_encode(array("code" => 100, "msg" => "get tradeno failed"));
//    exit();
//}
//$ip = $_SERVER['REMOTE_ADDR'];
//if(empty($ip)){
//    echo json_encode(array("code" => 200, "msg" => "get ip failed"));
//    exit();
//}
//$customerid = $_REQUEST['userid'];
//if(empty($customerid)){
//    echo json_encode(array("code" => 100, "msg" => "get user failed"));
//    exit();
//}
//$customerid = 1;
//$ip = "127.0.0.1";
//$tradeno = "20180622111111";
//$fee = 0.01;
if ((!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) || (!isset($_REQUEST['tradeno']) || empty($_REQUEST['tradeno']) || (!isset($_REQUEST['fee']) || empty($_REQUEST['fee'])))) {
    echo json_encode(array("code" => 100, "msg" => "request failed"));
    exit();
}
$customerid = $_REQUEST['userid'];
$ip = "139.196.7.165";
$tradeno = $_REQUEST['tradeno'];
$fee = $_REQUEST['fee'];

if ($fee == 0) {
    $order = $ordercom->getOrderByTradeno($tradeno);
    $res = $ordercom->changeOrderisPay($order['id'], 1);
    if ($res) {
        
    } else {
        echo json_encode(array("code" => 400, "msg" => "change ispay fail"));
        exit();
    }
    $servicelist = $serviceinfocom->getServiceinfoByOrderid($order['id']);
    foreach ($servicelist as $v) {
        $cservice = $cservicecom->getCservice($order['customer_id'], $v['service_id']);
        if (!empty($cservice)) {
            $number = $cservice['number'] + $v['total'];
            $res = $cservicecom->changeNumber($order['customer_id'], $v['service_id'], $number);
            if (!$res) {
                echo json_encode(array("code" => 400, "msg" => "change number fail customerid:".$order['customer_id']."service:".$v['service_id']));
                exit();
            }
        } else {
            $number = $v['total'];
            $res = $cservicecom->addCservice($order['customer_id'], $v['service_id'], $number);
            if(!$res) {
                echo json_encode(array("code" => 400, "msg" => "ADD number fail customerid:".$order['customer_id']."service:".$v['service_id']));
                exit();
            }
        }
    }
    echo json_encode(array("code" => 0, "msg" => "success fee 0"));
    exit();
}

$totalfee = $fee * 100;
$noncestr = mt_rand(time(), time() + rand());

$notifyurl = "https://" . $_SERVER['SERVER_NAME'] . "/Api/notify.php";
$payurl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

$customer = $customercom->getCustomerById($customerid);
$openid = $customer['openid'];

$params = array(
    "appid" => $appid,
    "mch_id" => $mch_id,
    "nonce_str" => $noncestr,
    "body" => "服务付费",
    "out_trade_no" => $tradeno,
    "total_fee" => $totalfee,
    "spbill_create_ip" => $ip,
    "trade_type" => "JSAPI",
    "notify_url" => $notifyurl,
    "openid" => $openid
);

$params['sign'] = $common->createSign($params, $paykey);
//var_dump($params);
$xml = $common->toxml($params);
$res = $common->call($payurl, $xml);
$time = time();
$returndate = $common->getcontent($res);
$log = new Logger("./../wxpay.log");
$log->debug($res);
$result = array("code" => 0, "msg" => "success", "data" => array());
if (!isset($returndate['result_code'])) {
    echo json_encode(array("code" => 500, "msg" => "create wxorder failed"));
    exit();
}
if ($returndate['result_code'] == 'SUCCESS') {
    $result["data"]["appId"] = $appid;
    $result["data"]["timeStamp"] = $time;
    $result["data"]["nonceStr"] = $noncestr;
    $result["data"]["package"] = "prepay_id=" . $returndate['prepay_id'];
    $result["data"]["signType"] = "MD5";
    $paysign = $common->createSign($result["data"], $paykey);
    $result["data"]["paySign"] = $paysign;
    echo json_encode($result);
} else {
    echo json_encode(array("code" => 500, "msg" => "create wxorder failed"));
}

exit();
